"""
#Copyright (c) 2008 Andrew Tamoney tamoneya AT gmail
#
#Permission is hereby granted, free of charge, to any person
#obtaining a copy of this software and associated documentation
#files (the "Software"), to deal in the Software without
#restriction, including without limitation the rights to use,
#copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the
#Software is furnished to do so, subject to the following
#conditions:
#
#The above copyright notice and this permission notice shall be
#included in all copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
#EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
#OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
#NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
#HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
#WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
#OTHER DEALINGS IN THE SOFTWARE.
"""
import sys, pygame, wordmixcore, time
pygame.init()
white = 255,255,255
green = 78,184, 72
starttime = 0
size = width, height = 800,600
large = pygame.font.SysFont("Times New Roman",60)
small = pygame.font.SysFont("Times New Roman",30)
guessrect = pygame.Rect(30,525,445,50)
guesspos = (45, 525)
wordrect = pygame.Rect(500,30,225,445)
timerect = pygame.Rect(500,525,225,50)
timepos = (510,530)
boardrect = pygame.Rect(30,30,445,445)
screen = pygame.display.set_mode(size)
background = pygame.image.load("background.png").convert()
maxtime = 180
guess = ""
newgame = 0
def main():
	global screen, starttime, white, green, background, small, large, size, guess, maxtime, newgame
	wordmixcore.init()
	wordmixcore.newgame()
	starttime = time.time()
	fill()
	while 1:
		#handle events
		#pygame.event.pump()
		for event in pygame.event.get():
			if event.type == pygame.QUIT: 
				sys.exit()
			if event.type == pygame.KEYDOWN:
				if (97 <= event.key <= 122): #a letter
					guess = guess + event.unicode
					#blit guess
					screen.blit(background, guessrect, guessrect)
					typed = small.render(guess, True, green)
					screen.blit(typed, guesspos)
				if (event.key == 8): # backspace
					guess = guess[:-1]
					#blit guess
					screen.blit(background, guessrect, guessrect)
					typed = small.render(guess, True, green)
					screen.blit(typed, guesspos)
				if (event.key == 13): #enter key
					if newgame == 1:
						wordmixcore.newgame()
						starttime = time.time()
						fill()
						newgame = 0
					else:
						screen.blit(background, guessrect, guessrect)
						if(wordmixcore.guess(guess)):
							#blit words
							i = 0
							screen.blit(background, wordrect, wordrect)
							for word in wordmixcore.user:
								i = i + 1
								if i < 11:
									answer = small.render(wordmixcore.user[i-1], True, green)
									screen.blit(answer, (510, i*40))
					guess = ""
				if (event.unicode == 27):#escape key
					pygame.event.post(pygame.QUIT)
		#check time
		now = time.time()
		if(now - starttime > maxtime): # 3 minutes
			end()
		else:
			#blit time remaining
			screen.blit(background, timerect, timerect)
			remaining = str(maxtime+starttime - now)
			seconds = small.render(remaining, True, white)
			screen.blit(seconds, timepos)
		#flip screen
		pygame.display.update()
def fill():
	global large, screen, white, background, maxtime
	screen.blit(background, (0,0))
	for i in range(4):
		for j in range(4):
			letter = large.render(wordmixcore.board[i][j], True, white)
			screen.blit(letter, (60 + i*110, 40 + j*110))
	clock = small.render(str(maxtime), True, white)
	screen.blit(clock, timepos)
	pygame.event.pump()
	pygame.display.update()

def end():
	global large, screen, green, background, starttime, newgame
	screen.blit(background, (0,0))
	message = str(wordmixcore.score(wordmixcore.user)) + " pts! Press Enter to Continue"
	result = small.render(message, True, green)
	screen.blit(result, guesspos)
	newgame = 1
	pygame.event.pump()
main()
